package com.example.piece.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.example.base.broadcast.Event
import com.example.base.broadcast.EventBusUtil
import com.example.base.ext.getViewModel
import com.example.base.ui.fragment.BaseFragment
import com.example.base.vm.onError
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.databinding.FragmentFansBinding
import com.example.piece.databinding.ItemFansBinding
import com.example.piece.entity.UserAttentionListEntity
import com.example.piece.ui.adapter.FansAdapter
import com.example.piece.ui.adapter.MsgAdapter
import com.example.piece.vm.UserViewModel


/**
 *粉丝列表
 */
class FansFragment : BaseFragment<FragmentFansBinding>() {
    override fun getContentRes(): Int = R.layout.fragment_fans

    companion object {
        fun newInstance(targetUserId: String): Fragment {
            val fragment = FansFragment()
            val bundle = Bundle()
            bundle.putString("targetUserId", targetUserId)
            fragment.arguments = bundle
            return fragment
        }
    }

    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }
    var page = 1
    var targetUserId = ""

    private val adapter: FansAdapter by lazy {
        val adapter = FansAdapter()
        mBinding.rv?.adapter = adapter
        adapter
    }

    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        targetUserId = arguments?.get("targetUserId") as String

        val loadMoreModule = adapter.loadMoreModule
        loadMoreModule.setOnLoadMoreListener {
            getList()
        }
        adapter.setEmptyView(R.layout.layout_empty)
        adapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                userViewModel.userAttention(this@FansFragment,
                    "0",
                    (adapter.getItem(position) as UserAttentionListEntity).userId) {
                    if (it.code == 200) {
                        adapter.removeAt(position)
                        EventBusUtil.sendEvent(Event<String>(Key.EventKey.EVENT_SINGIN,""))
                    }

                } onError {

                }
            }

        })
        mBinding?.srl?.setOnRefreshListener {
            page = 1
            loadMoreModule.isEnableLoadMore = true
            loadMoreModule.isAutoLoadMore = true
            getList()
        }
        getList()
    }

    private fun getList() {
        userViewModel.userFansList(this, page.toString(), targetUserId.toString()) {
            mBinding?.srl?.isRefreshing = false
            adapter.loadMoreModule.loadMoreComplete()

            if (it.code == 200) {
                page++
                if (it.data.isNullOrEmpty()) {
                    adapter.loadMoreModule.isEnableLoadMore = false
                    adapter.loadMoreModule.isAutoLoadMore = false
                    return@userFansList
                }
                if (page == 2) {
                    adapter.setList(it.data)

                } else {
                    adapter.addData(it.data)

                }

            }
        } onError {

        }
    }

}